package com.tql.kingdee.service;

import com.ruoyi.common.datasource.annotation.Master;
import com.tql.kingdee.bean.SysDeptBean;
import com.tql.kingdee.response.OrgResponse;
import com.tql.kingdee.mapper.SysDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service

public class SysDeptService {
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Transactional
    @Master
    public void saveAllOrg(List<OrgResponse> list, int flag, String pFnumber) {
        if(0==flag)
        sysDeptMapper.deleteAllOrg();
        if(1==flag) sysDeptMapper.deleteSysDept(pFnumber);
        List<SysDeptBean> list1= getSysDeptList(list,flag);
        sysDeptMapper.batchAdd(list1);
    }



















    private List<SysDeptBean> getSysDeptList(List<OrgResponse> list,int flag) {
        List<SysDeptBean> depts=new ArrayList<>();
        for(OrgResponse org:list)
        {
            SysDeptBean bean=getDept(org, flag);
            depts.add(bean);
        }

        return depts;
    }

    private SysDeptBean getDept(OrgResponse org,int flag) {
        SysDeptBean bean=new SysDeptBean();
        //这个字段是用来和哗啦啦店铺关联
       // bean.setRelateId(org.getFnumber());
        bean.setDeptCode(org.getFnumber());
        bean.setDeptName(org.getFname());
        bean.setPdeptCode(org.getParentFnumber());
        bean.setPdeptName(org.getParentFname());
        bean.setFlag(flag);
        return bean;
    }
}
